package HelperFinancial.User;

import HelperFinancial.Class.Credito;
import HelperFinancial.DataBase.CreditoAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class GanhoViewActivity extends Activity {
	private GanhoViewActivity frame = this;
	private TextView date;
	private TextView name;
	private TextView value;
	private TextView mesCorrente;
	
	private Credito ganho;
	
	private CreditoAdapter adapter;
	


	/** Called when the activity is first create */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ganho_view);
		
		adapter = CreditoAdapter.getInstance(frame);
		
		ganho = adapter.getCreditById(getIntent().getIntExtra("id", 0));

		date = (TextView) findViewById(R.id.date_ganho_view);
		date.setText(ganho.getDateDuration());
		name = (TextView) findViewById(R.id.name_ganho_view);
		name.setText(ganho.getName());
		value = (TextView) findViewById(R.id.value_ganho_view);
		value.setText(ganho.getValue()+"");
		mesCorrente = (TextView) findViewById(R.id.corrente_ganho_view);
		mesCorrente.setText(ganho.getQntdCorrente()+"");
		
		Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int width = display.getWidth();
		
		Button delete = (Button) findViewById(R.id.delete_ganho_view);
		delete.setOnClickListener(onDelete);
		delete.setWidth(width/2);
		Button update = (Button) findViewById(R.id.update_ganho_view);
		update.setOnClickListener(onUpdate);
		update.setWidth(width/2);

	}
		private View.OnClickListener onDelete = new View.OnClickListener() {
			public void onClick(View arg0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(frame);
				builder.setMessage("Deseja realmente excluir?")
						.setCancelable(false)
						.setPositiveButton("Yes",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										if (ganho.getQntdMeses() == 1 || ganho.getQntdCorrente() == ganho.getQntdMeses()) 
											adapter.removeCredit(ganho.getId());
										else
											adapter.removeFixa(ganho.getQntdCorrente(), ganho.getIdPai());
										finish();
										//startActivity(new Intent(frame, ExtractActivity.class));
									}
								})
						.setNegativeButton("No",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										dialog.cancel();
									}
								});
				AlertDialog alert = builder.create();
				alert.show();

			}
		};
		
		private View.OnClickListener onUpdate = new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(frame, CreditoUpdateActivity.class);
				Integer id = ganho.getId();
				if (id != null && id > 0) {
					intent.putExtra("id", id);
					finish();
					startActivity(intent);
				}

			}

		};

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}